package rasterprimate;

import java.util.ArrayList;

import repast.simphony.context.Context;
import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.engine.schedule.ScheduledMethod;
import repast.simphony.parameter.Parameters;
import repast.simphony.space.continuous.ContinuousSpace;
import repast.simphony.space.gis.Geography;
import repast.simphony.space.grid.Grid;
import repast.simphony.util.ContextUtils;
import repast.simphony.valueLayer.GridValueLayer;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Polygon;

//this represents a resource grid on the landscape
public class Resource {
	
	Context context;
	Geography geog;
	ContinuousSpace space;
	Coordinate coord;
	int gridX,gridY;
	Polygon geom;
	double resLevel = 0;		//resource level within polygon
	double maxResource;			//maximum resource level within the polygon
	double regrowRate;			//rate at which resources regrow
	double xExtent,yExtent;		//extent of polygon
	int remembered=0;
	int pointCount=0;			//used to count the number of marker points within a resource square
	int bbCellx,bbCelly;
	ArrayList<Integer> primatesID = null;
	
	
	@SuppressWarnings("unchecked")
	public Resource (Context con,double x,double y,double r,int gx, int gy){ 
		
		
		//get/set paramters
		Parameters p = RunEnvironment.getInstance().getParameters();
		context = con;
		context.add(this);
		geog = (Geography)context.getProjection("geog");
		space = (ContinuousSpace)context.getProjection("space");
		coord = new Coordinate(x,y);
		resLevel = r;
		regrowRate = (Double)p.getValue("regrowRate");
		maxResource = r;
		pointCount=0;
		gridX = gx;
		gridY = gy;
		
		
		//create its geometry
		GeometryFactory fac = new GeometryFactory();
		Coordinate cTR = new Coordinate(x+(Constants.cellSize/(2.0)),y+(Constants.cellSize/(2.0)));//top right
		Coordinate cTL = new Coordinate(x-(Constants.cellSize/(2.0)),y+(Constants.cellSize/(2.0)));//top left
		Coordinate cBL = new Coordinate(x-(Constants.cellSize/(2.0)),y-(Constants.cellSize/(2.0)));//bottom left
		Coordinate cBR = new Coordinate(x+(Constants.cellSize/(2.0)),y-(Constants.cellSize/(2.0)));//botton right
		Coordinate[] boundingCoords = {cTR,cTL,cBL,cBR,cTR};
		LinearRing ring = fac.createLinearRing(boundingCoords);
		
		geom = fac.createPolygon(ring, null);
		geom.buffer(100);
		geog.move(this, geom);//sets the polygon onto the geography surface
	}
	
	
	@ScheduledMethod(start=0)
	private void setupContext(){
		//get/set paramters
		Parameters p = RunEnvironment.getInstance().getParameters();
		context = ContextUtils.getContext(this);
		context.add(this);
		geog = (Geography)context.getProjection("geog");
		Geometry geom = geog.getGeometry(this);
		this.setCoord(geom.getCentroid().getCoordinate());

		regrowRate = (Double)p.getValue("regrowRate");
	}
	
	@ScheduledMethod(start=2, interval = 1,priority=1)
	public void step(){
		
		//check to see if resource is at max levels
		if (getresLevel()!=maxResource){
			//the resource will grow back until it's maximum level is reached
			if (getresLevel()<maxResource-regrowRate){
				setresLevel(getresLevel()+regrowRate);
			} else {
				setresLevel(maxResource);
			}
			//will degenerate if above max resource level
			if(getresLevel()>maxResource+regrowRate){
				setresLevel(getresLevel()-regrowRate);
			}else if (getresLevel()>maxResource && getresLevel()<maxResource+regrowRate){
				setresLevel(maxResource);
			}
		}
	}
	
	public double eatMe(double bite){
		double biteSize =0;
		//this method is used by primates foraging from this resource
		if (resLevel - bite > 0){
				setresLevel(resLevel - bite);
				biteSize =bite;
		}else{
			biteSize = resLevel;
			setresLevel(0);
		}
		
		return biteSize;
	}
	
	public void init(double x, double y, double res){
		setresLevel(res);
		setmaxResLevel(res);
		setCoord(new Coordinate(x,y));
	}
	public void setBBcell(int x,int y){
		bbCellx=x;
		bbCelly=y;
	}
	public int getBBcellX(){
		return bbCellx;
	}
	public int getBBcelly(){
		return bbCelly;
	}
	public void setresLevel(double r){
		resLevel = r;
	}
	public double getresLevel(){
		return resLevel;
	}
	public Coordinate getCoord(){
		return coord;
	}
	public void setCoord(Coordinate c){
		coord = c;
	}
	public double getcoordX(){
		return getCoord().x;
	}
	public double getcoordY(){
		return getCoord().y;
	}
	public int getGridx(){
		return gridX;
	}
	public int getGridy(){
		return gridY;
	}
	public void setmaxResLevel(double i){
		maxResource=i;
	}
	public double getmaxResLevel(){
		return maxResource;
	}
	public void setRemembered(int t){
		remembered=t;
	}
	public int getRemembered(){
		return remembered;
	}
	public void addPointCount(){
		pointCount++;
	}
	public int getPointCount(){
		return pointCount;
	}
	public void setPointCount(int count){
		pointCount = count;
	}
	public Polygon getGeom(){
		return geom;
	}
	public void setGeom(Polygon g){
		geom = g;
	}
}
